package com.theme.passport.controller.app;


import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.constant.ThemeNtsEnum;
import com.theme.passport.entity.constant.ThemeStatusEnum;
import com.theme.passport.entity.constant.WidgeSizeEnum;
import com.theme.passport.entity.params.WidgePreviews;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.service.theme.WidgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * APP未上架预览接口
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2024-07-14 11:50:56
 */
@RestController
@RequestMapping("app/preview")
@Api("APP 未上架功能预览接口")
public class AppPreviewController {

    @Autowired
    private WidgeService widgeService;

    @GetMapping("newest")
    @ApiOperation("获取最新组件[时间排序]")
    public R newest(@RequestParam(value = "page", defaultValue = "1") String page,
                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.SHELVES.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("themeShow", false);
        params.put("page", page);
        params.put("showStatus", 1);
        params.put("limit", limit);
        PageUtils pages = widgeService.queryPageByCreateAesc(params);
        List<WidgeEntity> listTemp = (List<WidgeEntity>) pages.getList();
        if (listTemp == null || listTemp.size() <= 0) {
            return R.ok().put("page", pages);
        }
        List<Long> widgeIds = new ArrayList<>(16);
        listTemp.forEach(widgeEntity -> {
            widgeIds.add(widgeEntity.getId());
        });
        params.put("ids", widgeIds);
        params.put("page", page);
        PageUtils pages2 = getWidgesList(params);
        return R.ok().put("page", pages2);
    }


    @GetMapping("newest/v2")
    @ApiOperation("获取最新组件[时间排序]V2")
    public R newestV2(@RequestParam(value = "page", defaultValue = "1") String page,
                      @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.SHELVES.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("themeShow", false);
        params.put("page", page);
        params.put("limit", limit);
        params.put("showStatus", 1);
        PageUtils pages = widgeService.queryPageByCreateAesc(params);
        List<WidgeEntity> listTemp = (List<WidgeEntity>) pages.getList();
        if (listTemp == null || listTemp.size() <= 0) {
            return R.ok().put("page", pages);
        }
        List<Long> widgeIds = new ArrayList<>(16);
        listTemp.forEach(widgeEntity -> {
            widgeIds.add(widgeEntity.getId());
        });
        params.put("ids", widgeIds);
        params.put("page", page);
        PageUtils pages2 = getWidgesListV2(params);
        return R.ok().put("page", pages2);
    }



    @GetMapping("newProduct")
    @ApiOperation("获取最新品组件[时间排序]")
    public R newProduct(@RequestParam(value = "page", defaultValue = "1") String page,
                        @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.SHELVES.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("showStatus", 1);
        params.put("themeShow", false);
        params.put("nts", String.valueOf(ThemeNtsEnum.YES.getValue()));
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = widgeService.queryPageByCreateAesc(params);
        List<WidgeEntity> listTemp = (List<WidgeEntity>) pages.getList();
        if (listTemp == null || listTemp.size() <= 0) {
            return R.ok().put("page", pages);
        }
        List<Long> widgeIds = new ArrayList<>(16);
        listTemp.forEach(widgeEntity -> {
            widgeIds.add(widgeEntity.getId());
        });
        params.put("ids", widgeIds);
        params.put("page", page);
        PageUtils pages2 = getWidgesList(params);
        return R.ok().put("page", pages2);
    }


    @GetMapping("newProduct/v2")
    @ApiOperation("获取最新品组件[时间排序]V2")
    public R newProductV2(@RequestParam(value = "page", defaultValue = "1") String page,
                          @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.SHELVES.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("themeShow", false);
        params.put("nts", String.valueOf(ThemeNtsEnum.YES.getValue()));
        params.put("showStatus", 1);
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = widgeService.queryPageByCreateAesc(params);
        List<WidgeEntity> listTemp = (List<WidgeEntity>) pages.getList();
        if (listTemp == null || listTemp.size() <= 0) {
            return R.ok().put("page", pages);
        }
        List<Long> widgeIds = new ArrayList<>(16);
        listTemp.forEach(widgeEntity -> {
            widgeIds.add(widgeEntity.getId());
        });
        params.put("ids", widgeIds);
        params.put("page", page);
        PageUtils pages2 = getWidgesListV2(params);
        return R.ok().put("page", pages2);
    }


    private PageUtils getWidgesList(Map<String, Object> params) {
        PageUtils pages = widgeService.queryPageBydistinctNameByIds(params);
        List<WidgeEntity> list = (List<WidgeEntity>) pages.getList();
        list.forEach(theme ->{
            //同名组件列表
            List<WidgeEntity> widgeSizeList = widgeService.queryWidgeListByName(theme.getName());
            WidgeEntity widge = widgeSizeList.get(0);
            BeanUtils.copyProperties(widge,theme);
            // 同名组件大小
            WidgePreviews widgePreviews = new WidgePreviews();
            widgeSizeList.forEach(item ->{
                String widgetSize = item.getWidgetSize();
                if (widgetSize.equals(WidgeSizeEnum.SMALL.getValue())){
                    widgePreviews.setSmall(item.getPreview());
                }
                if (widgetSize.equals(WidgeSizeEnum.MEDIUM.getValue())){
                    widgePreviews.setMidium(item.getPreview());
                }
                if (widgetSize.equals(WidgeSizeEnum.LAREGE.getValue())){
                    widgePreviews.setLarge(item.getPreview());
                }
            });
            theme.setWidgePreviews(widgePreviews);
            theme.setWidgeSizeList(widgeSizeList);
        });



        list = list.stream().sorted(Comparator.comparing(WidgeEntity::getUpdated).reversed()).collect(Collectors.toList());

        pages.setList(list);

        return pages;
    }




    private PageUtils getWidgesListV2(Map<String, Object> params) {
        PageUtils pages = widgeService.queryPageBydistinctNameByIdsV2(params);
        List<WidgeEntity> list = (List<WidgeEntity>) pages.getList();
        List<WidgeEntity> chilendList = new ArrayList<>();
        for (WidgeEntity theme : list) {
            List<WidgeEntity> widgeSizeList = widgeService.queryWidgeListByNameV2(theme.getName());
            chilendList.addAll(widgeSizeList);
        }
        for (WidgeEntity theme : list) {
            // 同名组件大小
            WidgePreviews widgePreviews = new WidgePreviews();
            for (WidgeEntity citem : chilendList) {
                if (theme.getName().equals(citem.getName())) {
                    String widgetSize = citem.getWidgetSize();
                    if (widgetSize.equals(WidgeSizeEnum.SMALL.getValue())) {
                        widgePreviews.setSmall(citem.getPreview());
                    }
                    if (widgetSize.equals(WidgeSizeEnum.MEDIUM.getValue())) {
                        widgePreviews.setMidium(citem.getPreview());
                    }
                    if (widgetSize.equals(WidgeSizeEnum.LAREGE.getValue())) {
                        widgePreviews.setLarge(citem.getPreview());
                    }
                    BeanUtils.copyProperties(citem, theme);
                }
            }
            theme.setWidgePreviews(widgePreviews);
        }
        List<WidgeEntity> returnList = new ArrayList<>();
        for (WidgeEntity item : list) {
            for (WidgeEntity citem : chilendList) {
                if (item.getName().equals(citem.getName())) {
                    WidgeEntity entity = new WidgeEntity();
                    BeanUtils.copyProperties(item, entity);
                    List<WidgeEntity> tempList = new ArrayList<>();
                    tempList.add(citem);
                    entity.setWidgeSizeList(tempList);
                    entity.setWidgetSize(tempList.get(0).getWidgetSize());
                    returnList.add(entity);
                }
            }
        }

        pages.setList(returnList);

        return pages;
    }

}
